using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Data;
using MediaAssistant.Data;
using MediaAssistant.DAL;

namespace MediaAssistant.Converters
{
    public class PrepareMusicConverter:IMultiValueConverter
    {
        #region Implementation of IMultiValueConverter

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var music = (Music)values[0];
            var dataSource = (LibraryDataSource) values[1];
            if(string.IsNullOrEmpty(music.Preferance))
            {
                PrepareMusic(music,dataSource.SelectedProfile);
            }
            return music.Preferance;
        }

        private static void PrepareMusic(Music music, Profile selectedProfile)
        {
            lock (DatabaseManager.LockObject)
            {
                music.Preferance = string.Format("{0:0.00}", DatabaseManager.Instance.GetScore(music, selectedProfile));
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}